package com.zbec.mdrs.web;

import com.zbec.framework.core.entity.SystemDictionary;
import com.zbec.framework.core.entity.SystemDictionaryType;
import com.zbec.framework.core.service.SystemDictionaryService;
import com.zbec.framework.core.service.SystemDictionaryTypeService;
import com.zbec.framework.core.vo.JqgridResponse;
import com.zbec.framework.core.vo.PageInfo;
import com.zbec.framework.security.entity.SysOrganization;
import com.zbec.framework.security.service.SecurityService;
import com.zbec.framework.security.vo.OrganizationVO;
import com.zbec.mdrs.constants.Constants;
import com.zbec.mdrs.entity.ColModelConfig;
import com.zbec.mdrs.entity.discount.Discount;
import com.zbec.mdrs.service.ColModelConfigService;
import com.zbec.mdrs.service.DiscountService;
import com.zbec.mdrs.service.LogsService;
import com.zbec.mdrs.util.ResourceUtils;
import com.zbec.mdrs.vo.DiscountVo;
import org.json.simple.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by fxl on 2015/10/28.
 */
@Controller
@RequestMapping(value = "/discount")
public class DiscountController extends BaseController {

    @Autowired
    private  DiscountService  discountService;

    @Autowired
    private LogsService logsService;

    @Autowired
    private SecurityService securityService;

    @Autowired
    private ColModelConfigService colModelConfigService;

    @RequestMapping(value = "/toDiscount")
    public ModelAndView toDiscount(HttpServletRequest request) {
        ModelAndView view=new ModelAndView();
        OrganizationVO orgVo= securityService.getCurrentUserVO().getBranch();
        if(orgVo!=null && orgVo.getOrgType().equals(Constants.BANK_TYPE_FH)){
            List<OrganizationVO> voList=securityService.findByParentOrgId(orgVo.getId());
            if(voList==null){
                voList=new ArrayList<OrganizationVO>();
            }
            voList.add(0,orgVo);
            view.addObject("orgList",voList);
            view.addObject("isFh","true");
        }else {
            view.addObject("isFh","false");
            if(orgVo!=null){
                view.addObject("orgId",orgVo.getId());
            }
        }
        if(this.isUserReseted(securityService.getCurrentUserVO())){
            view.setViewName("system/editPassword");
        }else{
            view.setViewName("discount/discount_list");
            String loginName=securityService.getCurrentUser().getLoginName();
            ColModelConfig config= colModelConfigService.findColModelConfig(loginName, Constants.BUSINESS_TYPE_DISCOUNT);
            view.addObject("config",config);
        }
        return view;
    }

    @RequestMapping(value = "/discountList")
    @ResponseBody
    public JqgridResponse<DiscountVo> findAllDiscountn(DiscountVo vo,PageInfo pageInfo,HttpServletRequest request,@RequestParam(value = "_search", defaultValue = "false") String isSearch) {
        vo.setSearch(isSearch);
        return discountService.findAllDiscount(vo, pageInfo);
    }

    @RequestMapping(value = "/toAddDiscount")
    public ModelAndView toAddLoan(HttpServletRequest request) {
        ModelAndView view =new ModelAndView("discount/add_discount");
        List<SysOrganization> voList=securityService.findAllOrgnization();
        view.addObject("orgList",voList);
        return view;
    }

    @RequestMapping(value = "/addDiscount")
    public void addLoan(DiscountVo discountVo){
        discountService.addDiscount(discountVo);
    }

    @RequestMapping(value = "/editDiscount")
    public void editDiscount(DiscountVo discount,HttpServletRequest request){
        String userIp=this.getIPAddress(request);
        discountService.updateLoan(discount,userIp);
    }

    @RequestMapping(value = "/ignoreDiscount")
    @ResponseBody
    public String ignoreDiscount(@RequestParam(value = "ids", defaultValue = "") String ids){

        discountService.ignoreDiscount(ids);
        JSONObject object=new JSONObject();
        object.put("result","true");
        return object.toJSONString();
    }

    @RequestMapping(value = "/toImportDiscount")
    public String toImportDiscount(HttpServletRequest request) {
        return "discount/discount_importUpdate";
    }

    @RequestMapping(value = "/importUpdate")
    public  void importUpdate(HttpServletRequest request,HttpServletResponse response,@RequestParam("importFile") MultipartFile file,@RequestParam("sjsj") String sjsj) throws Exception{
        JSONObject object=new JSONObject();
        String importFileFileName = file.getOriginalFilename();
        String userIp=this.getIPAddress(request);
        if (importFileFileName != null) {
            if (!importFileFileName.endsWith(".xls")&&!importFileFileName.endsWith(".xlsx")) {
                object.put("result","false");
                object.put("message", ResourceUtils.getLocaleString("message.importformatfailure"));
            }else{
                String message = discountService.importUpdate(file,sjsj,userIp);
                if (!message.equals("false")) {
                    object.put("result","true");
                    object.put("message", ResourceUtils.getLocaleString("message.importsuccess")+"==="+message);
                } else {
                    object.put("result","false");
                    object.put("message", ResourceUtils.getLocaleString("message.importfailure")+"===");
                    logsService.handleBuzLogData(userIp, "logs.type.24", "logs.operation.importUpdate", false, ResourceUtils.getLocaleString("message.importfailure"));
                }
            }
        }
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write(object.toJSONString());
    }


}
